package com.example.demo.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.user.entity.UUserAssets;
import com.example.demo.user.service.UUserAssetsService;
import com.example.demo.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户余额表; 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/uUserAssets")
@Tag(name = "uUserAssets", description = "用户余额表")
public class UUserAssetsController {
    @Autowired
    private UUserAssetsService uUserAssetsService;

    /**
     * 获取用户余额模块列表
     */
    @GetMapping("/getUAssetsList")
    @Operation(summary = "获取用户余额列表接口", description = "获取用户余额列表接口")
    public Result<?> getUAssetsList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                  @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                  UUserAssets uAssets){
        Page<UUserAssets> page = new Page<>(current,size);
        QueryWrapper<UUserAssets> queryWrapper = new QueryWrapper<>(uAssets);
        return Result.ok(uUserAssetsService.page(page,queryWrapper));
    }

    /**
     * 根据ID查询用户余额
     */
    @GetMapping("/getUAssetsById")
    @Operation(summary = "根据id查询用户余额明细", description = "根据id查询用户余额明细")
    public Result<?> getUAssetsById(Integer id){
        return Result.ok(uUserAssetsService.getById(id));
    }

    /**
     * 修改用户余额
     */
    @PutMapping("/updateUAssets")
    @Operation(summary = "根据id修改用户余额明细", description = "根据id修改用户余额明细")
    @Transactional
    public Result<?> updateUAssets(@RequestBody UUserAssets uAssets){
        return Result.ok(uUserAssetsService.updateById(uAssets));
    }

    /**
     * 新增用户余额
     */
    @PostMapping("/addUAssets")
    @Operation(summary = "新增用户余额明细", description = "新增用户余额明细")
    public Result<?> addUAssets(@RequestBody UUserAssets uAssets){
        return Result.ok(uUserAssetsService.save(uAssets));
    }

    /**
     * 删除用户余额
     */
    @DeleteMapping("/deleteUAssetsById")
    @Operation(summary = "删除用户余额", description = "删除用户余额")
    @Transactional
    public Result<?> deleteUAssetsById(Integer id){
        return Result.ok(uUserAssetsService.removeById(id));
    }

    /**
     * 根据用户查询用户余额
     */
    @GetMapping("/getUAssetsByUser")
    @Operation(summary = "根据用户查询用户余额", description = "根据用户查询用户余额")
    public Result<?> getUAssetsByUser(Integer userId){
        QueryWrapper<UUserAssets> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",userId);
        return Result.ok(uUserAssetsService.getOne(queryWrapper));
    }
}

